//============================================================================
// Name        : rectangle.cpp
// Author      : dfoelber
//============================================================================

// INCLUDES //
#include "physics/rectangle.h"
#include <iostream>
//////////////

using namespace std;

RectangleArea::RectangleArea(Path* path) : Area(RECTANGLE, path)
{
}

RectangleArea::RectangleArea(double x, double y, double width, double height) : Area(RECTANGLE, NULL)
, x(x)
, y(y)
, width(width)
, height(height)
{
}

RectangleArea::~RectangleArea()
{
}

int RectangleArea::Intersects( Area* area, double sourceX, double sourceY, double otherX, double otherY )
{
	int side = 0;
	
	if( area )
	{
		RectangleArea* rect;
		switch( area->GetId() )
		{
		case RECTANGLE:
			rect = (RectangleArea*)area;
			
			if( (int)(GetX() + GetWidth()) <= (int)(rect->GetX()) &&
				(int)(GetX() + GetWidth() + sourceX) > (int)(rect->GetX() + otherX) &&
				(int)(GetY() + GetHeight() + sourceY) > (int)(rect->GetY() + otherY) &&
				(int)(GetY() + sourceY) < (int)(rect->GetY() + rect->GetHeight() + otherY) )
				side += SIDE_RIGHT;
			else if( (int)(GetX() + GetWidth()) <= (int)(rect->GetX()) &&
				(int)(GetX() + GetWidth() + sourceX) > (int)(rect->GetX() + otherX) &&
				(int)(GetY() + GetHeight() + sourceY) == (int)(rect->GetY() + rect->GetHeight() + otherY) &&
				(int)(GetY() + sourceY) == (int)(rect->GetY() + otherY) )
				side += SIDE_RIGHT;
			if( (int)(GetX()) >= (int)(rect->GetX() + rect->GetWidth()) &&
				(int)(GetX() + sourceX) < (int)(rect->GetX() + rect->GetWidth() + otherX) &&
				(int)(GetY() + GetHeight() + sourceY) > (int)(rect->GetY() + otherY) &&
				(int)(GetY() + sourceY) < (int)(rect->GetY() + rect->GetHeight() + otherY) )
				side += SIDE_LEFT;
			else if( (int)(GetX()) >= (int)(rect->GetX() + rect->GetWidth()) &&
				(int)(GetX() + sourceX) < (int)(rect->GetX() + rect->GetWidth() + otherX) &&
				(int)(GetY() + GetHeight() + sourceY) == (int)(rect->GetY() + rect->GetHeight() + otherY) &&
				(int)(GetY() + sourceY) == (int)(rect->GetY() + otherY) )
				side += SIDE_LEFT;
			if( (int)(GetY() + GetHeight()) <= (int)(rect->GetY()) &&
				(int)(GetY() + GetHeight() + sourceY) > (int)(rect->GetY() + otherY) &&
				(int)(GetX() + GetWidth() + sourceX) > (int)(rect->GetX() + otherX) &&
				(int)(GetX() + sourceX) < (int)(rect->GetX() + rect->GetWidth() + otherX) )
				side += SIDE_BOTTOM;
			else if( (int)(GetY() + GetHeight()) <= (int)(rect->GetY()) &&
				(int)(GetY() + GetHeight() + sourceY) > (int)(rect->GetY() + otherY) &&
				(int)(GetX() + GetWidth() + sourceX) == (int)(rect->GetX() + rect->GetWidth() + otherX) &&
				(int)(GetX() + sourceX) == (int)(rect->GetX() + otherX) )
				side += SIDE_BOTTOM;
			if( (int)(GetY()) >= (int)(rect->GetY() + rect->GetHeight()) &&
				(int)(GetY() + sourceY) < (int)(rect->GetY() + rect->GetHeight() + otherY) &&
				(int)(GetX() + GetWidth() + sourceX) > (int)(rect->GetX() + otherX) &&
				(int)(GetX() + sourceX) < (int)(rect->GetX() + rect->GetWidth() + otherX) )
				side += SIDE_TOP;
			else if( (int)(GetY()) >= (int)(rect->GetY() + rect->GetHeight()) &&
				(int)(GetY() + sourceY) < (int)(rect->GetY() + rect->GetHeight() + otherY) &&
				(int)(GetX() + GetWidth() + sourceX) == (int)(rect->GetX() + rect->GetWidth() + otherX) &&
				(int)(GetX() + sourceX) == (int)(rect->GetX() + otherX) )
				side += SIDE_TOP;
			break;
		default:
			break;
		}
	}
	
	return side;
}

double RectangleArea::GetX()
{
	if( GetPath() )
		return GetPath()->GetX();
	else return x;
}

double RectangleArea::GetY()
{
	if( GetPath() )
		return GetPath()->GetY();
	else return y;
}

double RectangleArea::GetWidth()
{
	if( GetPath() )
		return GetPath()->GetWidth();
	else return width;
}

double RectangleArea::GetHeight()
{
	if( GetPath() )
		return GetPath()->GetHeight();
	else return height;
}
